#include <hilog/log.h>
#include <osg/Version>
#include <osg/GraphicsContext>
#include <osg/DisplaySettings>
#include <string>
#include <cstdio>

#define LOG_TAG "OSG_WRAPPER"

// 获取OSG版本
extern "C" const char* OSG_GetVersion() {
    static char version[256];
    
    const char* osgVersion = osgGetVersion();
    const char* libName = osgGetLibraryName();
    const char* releaseDate = osgGetReleaseDate();
    
    snprintf(version, sizeof(version), 
             "OSG Version: %s\n"
             "API: %d.%d.%d\n"
             "Library: %s\n"
             "Date: %s",
             osgVersion,
             OSG_VERSION_MAJOR,
             OSG_VERSION_MINOR,
             OSG_VERSION_PATCH,
             libName,
             releaseDate);
    
    OH_LOG_INFO(LOG_APP, "Get OSG version: %s", osgVersion);
    return version;
}

// 检查显示设置是否可用
extern "C" const char* OSG_CheckDisplay() {
    static char result[256];
    
    osg::DisplaySettings* ds = osg::DisplaySettings::instance();
    if (ds) {
        snprintf(result, sizeof(result),
                 "Display Settings: OK\n"
                 "Double Buffer: %s\n"
                 "Depth Bits: %d",
                 ds->getDoubleBuffer() ? "Yes" : "No",
                 ds->getMinimumNumAlphaBits());
    } else {
        snprintf(result, sizeof(result), "Display Settings: Not Available");
    }
    
    OH_LOG_INFO(LOG_APP, "Check display settings");
    return result;
}

// 检查窗口系统
extern "C" const char* OSG_CheckWindowSystem() {
    static char result[256];
    
    osg::GraphicsContext::WindowingSystemInterface* wsi = 
        osg::GraphicsContext::getWindowingSystemInterface();
    
    if (wsi) {
        unsigned int screens = wsi->getNumScreens();
        snprintf(result, sizeof(result),
                 "Window System: Available\n"
                 "Screens: %d",
                 screens);
    } else {
        snprintf(result, sizeof(result), "Window System: Not Available");
    }
    
    OH_LOG_INFO(LOG_APP, "Check window system");
    return result;
}

// 检查OpenGL支持
extern "C" const char* OSG_CheckOpenGL() {
    static char result[256];
    
    // 尝试创建简单的图形上下文来检查OpenGL支持
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    traits->width = 1;
    traits->height = 1;
    traits->windowDecoration = false;
    traits->pbuffer = true;  // 离屏渲染
    
    osg::ref_ptr<osg::GraphicsContext> gc = 
        osg::GraphicsContext::createGraphicsContext(traits.get());
    
    if (gc.valid()) {
        snprintf(result, sizeof(result), 
                 "OpenGL Support: Yes\n"
                 "Context Created: OK");
    } else {
        snprintf(result, sizeof(result), 
                 "OpenGL Support: No\n"
                 "Context Created: Failed");
    }
    
    OH_LOG_INFO(LOG_APP, "Check OpenGL support");
    return result;
}

// 初始化函数
extern "C" bool OSG_Initialize() {
    OH_LOG_INFO(LOG_APP, "OSG Initialize called");
    
    // 只需要确保DisplaySettings实例存在
    osg::DisplaySettings::instance();
    
    OH_LOG_INFO(LOG_APP, "OSG initialized successfully");
    return true;
}
